
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="attendance/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="attendance/css/commonly-used-show.css" />
    <script type="text/javascript" src="attendance/js/jquery.min.js"></script>
    <script type="text/javascript" src="attendance/js/bootstrap.js" ></script>

    <title>添加课程</title>

</head>
<body class="mybody">

<c:if test="${user_leve == 0}">
    <jsp:include page="theTopMenuAdmin.jsp" />
</c:if>

<c:if test="${user_leve == 1}">
    <jsp:include page="theTopMenuTeacher.jsp" />
</c:if>


<div class="container-fluid">
    <div class="row">

        <div class="col-md-12">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">
                            <div class="alert alert-success">


                                <div>
        <h3 style="text-align: center">添加课程信息</h3>
        <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3" />

        <div id="add_course" style="margin-left: 80px">

            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>年级：</label>
                <select class="form-control edit" title="请选择" id="tv_grade" data-live-search="true" >
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="2017">2017</option>
                    <option value="2018">2018</option>
                    <option value="2019">2019</option>
                     <option value="2020">2020</option>
                     <option value="2021">2021</option>
                </select>

            </div>


            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>学院：</label>
                <select class="form-control edit" title="请选择" id="tv_college" data-live-search="true" >
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="继续教育学院">继续教育学院</option>
                    <option value="国际学院">国际学院</option>
                    <option value="大健康学院">大健康学院</option>
                     <option value="材料与化学工程学院">材料与化学工程学院</option>
                     <option value="大数据学院">大数据学院</option>
                    <option value="农林工程与规划学院">农林工程与规划学院</option>
                    <option value="经济管理学院">经济管理学院</option>
                    <option value="艺术学院">艺术学院</option>
                    <option value="人文学院">人文学院</option>
                    <option value="教育学院">教育学院</option>
                    <option value="马克思主义学院">马克思主义学院</option>
                    <option value="乌江学院">乌江学院</option>
                     <option value="国学院">国学院</option>
                </select>

            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>班级：</label>
                <input type="text" id="tv_class_affairs" class="form-control edit" placeholder="请输入班级">
            </div>


            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>课程名称：</label>
                <input type="text" id="course_name" class="form-control edit" placeholder="请输入课程名称" >
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>任课老师：</label>
                <input type="text" id="instructor" class="form-control edit" placeholder="请输入任课老师">
            </div>


            <div class="form-group same-line-left">
                <label>XXXX</label>
                <input type="text" disabled="disabled" class="form-control edit" >
            </div>


            <div class="form-group same-line">
                <label><b style="color: red">*&nbsp</b>课程简介：</label>
                <textarea id="txt_course_intr" style="height: 150px" class="form-control edit" placeholder="请输入课程简介" ></textarea>

            </div>


            <div class="form-group same-line-left">
                <label><b style="color: red">*&nbsp</b>课程图片：</label>
                <div>
                    <img id="image1" src="attendance/img/uploadly.png" width="150px" height="150px" >
                    <input type="file" onChange="selectImage(this)" />
                </div>
            </div>


        </div>

        <div class="form-group text-center same-line-clear">
            <input type="button" value="添加" class="btn btn-primary" onclick="addCourse()">
            <input type="reset" value="重置" class="btn btn-danger interval" onclick="reset()">
        </div>

        <div class="text-center">
            <label id="user_null" style="color: red"></label>
        </div>

    </div>


                            </div>
                        </div>
                    </div>
                </div>
        </div>



    </div>

 </div>


<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    var userNull = document.getElementById("user_null");
    var graphic;
    if (userdt.result.userName != null){
        $("#usname").text(userdt.result.userName);
    }else {
        $("#usname").text(userdt.result.userLoginName);
    }

    function reset(){
        var addUser = document.getElementById("add_course");
        var ipts = addUser.getElementsByTagName("input");
        for (var i = 0; i < ipts.length; i++){
            ipts[i].value = "";
        }
    }


    var tvGrade;
    var tvCollege;
    var tvClassAffairs;
    var courseName;
    var instructor;
    var txtCourse;

    function addCourse(){

        tvGrade = $("#tv_grade").val();
        tvCollege = $("#tv_college").val();
        tvClassAffairs = $("#tv_class_affairs").val();
        courseName = $("#course_name").val();
        instructor = $("#instructor").val();
        txtCourse = $("#txt_course_intr").val();


        if ($.isEmptyObject(tvGrade)){
            userNull.innerHTML = "年级不能为空";
            return false;
        }else if ($.isEmptyObject(tvCollege)){
            userNull.innerHTML = "学院不能为空";
            return false;
        }else if ($.isEmptyObject(tvClassAffairs)){
            userNull.innerHTML = "班级不能为空";
            return false;
        }else if ($.isEmptyObject(courseName)){
            userNull.innerHTML = "课程名称不能为空";
            return false;
        }else if ($.isEmptyObject(instructor)){
            userNull.innerHTML = "任课老师不能为空";
            return false;
        }else if ($.isEmptyObject(txtCourse)){
            userNull.innerHTML = "课程简介不能为空";
            return false;
        }else if ($.isEmptyObject(graphic)){
            userNull.innerHTML = "课程图片不能为空";
            return false;
        }else {
            userNull.innerHTML = "";
        }



        var postUrl = "${pageContext.request.contextPath}/uploadFile";
        var formData = new FormData();
        formData.append("userId",userdt.result.createUserId);
        formData.append("pictureUrl",graphic);    //生成一对表单属性
        $.ajax({
            type: "post",           //因为是传输文件，所以必须是post
            url: postUrl,         //对应的后台处理类的地址
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    addCourseDate(data.result);
                }
            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });


    }

    function addCourseDate(mypicture){

        var postUrl = "${pageContext.request.contextPath}/member/courseInsert";
        var str = {
            "teacherId": userdt.result.id,
            "grade": tvGrade,
            "college": tvCollege,
            "classAndGrade": tvClassAffairs,
            "courseTitle": courseName,
            "courseIntroduction": txtCourse,
            "classPictures": mypicture.pictureUrl,
            "instructor": instructor
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    //alert("添加用户成功")
                    if (${user_leve == 0}){
                        location.href = "${pageContext.request.contextPath}/adminCourseInformationManagement";
                    }else {
                        location.href = "${pageContext.request.contextPath}/courseInformationManagement";
                    }

                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }


    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById("image1").src = evt.target.result;
            picture = evt.target.result;
        }
        //通过文件流将文件转换成Base64字符串
        reader.readAsDataURL(file.files[0]);
        graphic = file.files[0];
    }

</script>

</body>
</html>
</span>